package com.ccys.doctor.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.TranslationUtil;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.entity.TblDoctor;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.mapper.TblMemberMapper;
import com.ccys.db.page.AppPageDto;
import com.ccys.db.vo.doctor.consultationHistory.BookingsMakeVo;
import com.ccys.db.vo.doctor.consultationHistory.GetConsultationHistoryVo;
import com.ccys.doctor.service.ConsultationHistoryService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: WhiteCoat
 * @description: 咨询历史接口
 * @author: ZhangJingCan
 * @create: 2024-02-06 15:58
 **/

@Service
public class ConsultationHistoryServiceImpl implements ConsultationHistoryService {

    @Resource
    private TblBookingMapper tblBookingMapper;

    @Resource
    private TblMemberMapper tblMemberMapper;

    @Override
    public ResponseResultApp<GetConsultationHistoryVo> getConsultationHistory(Integer pageIndex, Integer pageSize, String startDate, String endDate, String keyword) {
        //用于初始化时间戳 - 和数据库做对比用
        Long startDateTime = null;
        Long endDateTime = null;
        //格式化字符串
        Date start = Kit.convertToDate(startDate, "yyyy/MM/dd");
        Date end = Kit.convertToDate(endDate, "yyyy/MM/dd");
        if (ObjectUtil.isNotEmpty(start)) {
            startDateTime = start.getTime();
        }
        if (ObjectUtil.isNotEmpty(end)) {
            endDateTime = end.getTime();
        }
        TblDoctor doctor = UserUtil.getCurLoginDoctor();
        //查询数据
        AppPageDto.startPage();
        List<BookingsMakeVo> list = tblBookingMapper.getConsultationHistory(doctor.getId(), startDateTime, endDateTime, keyword);
        PageInfo<BookingsMakeVo> pageInfo = new PageInfo<>(list);
        list.forEach(item -> {
            //设置从创建至今过去了多少天
            item.setCreatedOnDisplay("about " + Kit.calculateDaysSinceDate(item.getCreatedOn()) + " days ago");
            //翻译
            String trans = TranslationUtil.getContentLastLanguage(item.getDiagnosis(), tblMemberMapper.selectByPrimaryKey(item.getMember().getId()).getCurrentLanguage());
            item.setDiagnosisTrans(StringUtils.isBlank(trans) ? item.getDiagnosis() : trans);
        });
        //准备返回对象
        GetConsultationHistoryVo vo = new GetConsultationHistoryVo();
        vo.setTotalPage(pageInfo.getPages());
        vo.setBookings(list);
        vo.setPatientsServed(tblBookingMapper.getPatientServiceCountByDoctorId(doctor.getId()));
        vo.setThisMonth(tblBookingMapper.getThisMonthServiceCountByDoctorId(doctor.getId()));
        return ResponseResultApp.success(vo);
    }

    @Override
    public ResponseResultApp<List<String>> getIdConsultationHistory(String startDate, String endDate, String keyword) {
        Date start = null;
        Date end = null;
        if (ObjectUtil.isNotEmpty(startDate)) {
            start = Kit.convertToDate(startDate, "yyyy/MM/dd");
        }
        if (ObjectUtil.isNotEmpty(endDate)) {
            end = Kit.convertToDate(startDate, "yyyy/MM/dd");
        }
        TblDoctor doctor = UserUtil.getCurLoginDoctor();
        //这里就是查询当前医生的booking表 - 并且keyword就是模糊查询的问诊的member的名字,返回的是booking的ID
        List<String> list = tblBookingMapper.getIdConsultationHistory(start, end, keyword, doctor.getId());
        return ResponseResultApp.success(list);
    }

}
